package zero.tech.games.gate.topic;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import zero.tech.core.data.Resp;
import zero.tech.games.common.Code;
import zero.tech.games.common.PipeiGroup;
import zero.tech.games.common.TopicOrder;
import zero.tech.games.common.data.RoomNode;
import zero.tech.games.gate.data.GATECMD;
import zero.tech.games.gate.util.PackageUtil;
import zero.tech.redis.topic.TopicChannelType;
import zero.tech.redis.topic.TopicProcess;
import zero.tech.wsnet.controller.CacheOnline;
import zero.tech.wsnet.controller.Controller;
import zero.tech.wsnet.data.Message;

public class ClubMemberNotifyEnterGameTopic extends TopicProcess{

	@Override
	public String orderType() {
		return TopicOrder.NOTIFY_ENTER_GAME;
	}

	@Override
	public void process(String message) throws Exception {
		JSONObject obj=JSONObject.parseObject(message);
		String room_nodes=obj.getString("room_nodes");
		String groups_str=obj.getString("groups");
		int fzb_playernum=obj.getIntValue("fzb_playernum");
		List<PipeiGroup> groups=JSONObject.parseObject(groups_str, new TypeReference<List<PipeiGroup>>() {});
		Map<String,RoomNode> map=JSONObject.parseObject(room_nodes, new TypeReference<Map<String,RoomNode>>(){});
		for(PipeiGroup group:groups) {
			String id=group.getId();
			RoomNode roomNode=map.get(id);
			if(roomNode!=null) {
				for(Integer uid:group.getSimpleUsers().keySet()) {
					Controller controller= CacheOnline.getCache().getOnlines().get(uid);
					if(controller!=null&&controller.getWebClient()!=null&&controller.getWebClient().isAlive()) {
						controller.getWebClient().sendMessage(Message.build(GATECMD.NOTIFY_ENTER_GAME, Resp.build(Code.OK, "通知进入游戏",PackageUtil.packageNotifyEnterGame(roomNode,fzb_playernum))));
					}
				}
			}
		}
	}

	@Override
	public TopicChannelType channelType() {
		return TopicChannelType.CLUB_MEMBER_PIPEI_NOTIFY_ENTER_GAME;
	}

}
